<?php

use Core\Request;

class _config extends base
{
    //要先删除 router/cache.php 再执行此脚本
    public function make_cache()
    {
        $env = Request::Route('env');
        $files = scandir(CONFIGPATH);
        sort($files);

        $cache_file = "config_cache_$env";
        $arr = [];
        $arr[] = '<?php'.PHP_EOL.'$GLOBALS["'.$cache_file.'"] = [';

        foreach ($files as $file) {
			$i = pathinfo($file);
			$name = $i['filename'];
			$ext = $i['extension'];
			if ($ext == 'php' && strpos($name, 'config_cache_') === false) {
				include(CONFIGPATH.$file);
                $str = var_export($GLOBALS['config'][$name], true);
                $str = str_replace('\\\\', '\\', $str);
				$arr[] = "'$name' => " . $str . ','.PHP_EOL;
			}
		}
        $arr[] = '];';
        file_put_contents(CONFIGPATH.$cache_file.'.php', implode(PHP_EOL, $arr));
        echo 'ok';
    }
}